<?php

class userAction extends BaseAction {
    protected $layout = 'admin';
    protected $theme  = '/theme';
    protected $nologin= array('login','join_us','reg_submit','check_email','test','logout','view');
    
    public function execute() {
        //$this->view->setLayoutPath('admsssins');

        $this->view->layout = 'account';
        if ($_SESSION['user'] == '' && !in_array($this->params['action'], $this->nologin)) {
            $this->view->layout = '';
            $this->redirect('/');
        }

        if(array_key_exists('sina_id', $_SESSION) && !in_array($this->params['action'], array('check_email','reset_password','logout'))) {
            $this->redirect('home/sina_login/');
        }
        $this->view->setLayoutPath(ROOT.$this->theme.'/admin/layout');
        parent::execute();
    }

    public function actionView(){
        $this->view->layout = 'user';
        $this->setPageTitle('统计信息');
        $uhash  = $this->params['uhash'];
        if(empty($uhash)) {
            $uhash  = str_replace('/' ,'', $_SERVER['REQUEST_URI']);
        }
        $this->set('uhash', $uhash);
        $this->render();
    }
    /**
     * 注册
     */
    public function actionJoinUs() {
        //$this->render();
        if(!empty($_POST)){
            $email  = $this->postVar('email', 'email');
            $pass  = $this->postVar('password');
            $pass   = Common::getPassword($pass);
            $user   = DB_USER::hasEmail($email);
            if($user){
                echo 'error';
            }else{
                $user   = new DB_USER();
                $user->setEmail($email);
                $user->setPassword($pass);
                $user->save();
                $user   = $user->getArray();
                //保存状态
                $_SESSION['user']   = $user;
                $ip = getIp();
                //记录登陆日志
                DB_USER_LOG::writeLog($user['id'], $ip);
                get_block('home', 'set_url_uid');
                echo 'ok';
            }
        }
    }

    public function actionCheckEmail() {
        header('Content-type:text/html;charset:utf-8');
        $this->view->layout = '';
        $email  = $this->postVar('email');
        $user   = DB_USER::hasEmail($email);
        if($user){
            echo 'error';
        }else{
            echo 'ok';
        }
    }

    public function actionStats(){
        $total  = DAO_Finder::findCount(DB_URL, array('uid' =>$_SESSION['user']['id']));
        $this->setPageTitle('短网址统计信息');
        $this->set('total', $total);
        $this->render();
    }

    /**
     * 访问概况
     */
    public function actionOverView() {
        $this->setPageTitle('访问概况');
        $id  = $this->params['id'];
        if($id) {
            $rs = DAO_Finder::findById(DB_URL, $id);
            if(empty($rs)) {
                $uhash  = '';
            }else{
                $uhash  = $rs->getUhash();
            }
            
            //今日PV
            $today  = DAO_Finder::daoOne(DB_COUNT_PV, array('log_time'=>date('Ymd'),'uhash' =>$uhash));
            if(!$today) {
                $today = 0;
            }else{
                $today  = $today->getPv();
            }
            $this->set('today_pv', $today);

            //今日IP
            $today  = DAO_Finder::daoOne(DB_COUNT_IP, array('log_time'=>date('Ymd'),'uhash' =>$uhash));
            if(!$today) {
                $today = 0;
            }else{
                $today  = $today->getPv();
            }
            $this->set('today_ip', $today);

            $yes    = date("Ymd") -1;
            //昨日IP
            $yes_today  = DAO_Finder::daoOne(DB_COUNT_IP, array('log_time'=>$yes,'uhash' =>$uhash));
            if(!$yes_today) {
                $yes_today = 0;
            }else{
                $yes_today  = $yes_today->getPv();
            }
            $this->set('yesday_ip', $yes_today);

            //昨日PV
            $yes_today  = DAO_Finder::daoOne(DB_COUNT_PV, array('log_time'=>$yes,'uhash' =>$uhash));
            if(!$yes_today) {
                $yes_today = 0;
            }else{
                $yes_today  = $yes_today->getPv();
            }
            $this->set('yesday_pv', $yes_today);

            //最高pv
            $total_ip   = DAO_Finder::daoOne(DB_COUNT_IP, null,'*', 'ip desc');
            if(!$total_ip) {
                $total_ip   = new DB_COUNT_IP();
                $total_ip->setLogTime(date('Ymd'));
                $total_ip->setIp(1);
            }
            $this->set('total_ip', $total_ip);
            //最高pv
            $total_pv   = DAO_Finder::daoOne(DB_COUNT_PV, null,'*', 'pv desc');
            if(!$total_pv) {
                $total_pv   = new DB_COUNT_PV();
                $total_pv->setLogTime(date('Ymd'));
                $total_pv->setPv(1);
            }
            $this->set('total_pv', $total_pv);

            $this->set('rs', $rs);
        }

        $this->set('id', $id);
        $this->render();
    }

    public function actionCate() {
        $this->setPageTitle('全部短网址');
        $this->render();
    }

    /**
     * 日段统计
     */
    public function actionMonthCount() {
        $this->setPageTitle('月段统计');
        $id  = $this->params['id'];
        if($id) {
            $rs = DAO_Finder::findById(DB_URL, $id);
            $this->set('rs', $rs);
        }

        $this->set('id', $id);
        $this->render();
    }

    public function actionHourCount() {
        $this->setPageTitle('时段统计');
        $id  = $this->params['id'];
        if($id) {
            $rs = DAO_Finder::findById(DB_URL, $id);
            $this->set('rs', $rs);
        }

        $this->set('id', $id);
        $this->render();
    }

    public function actionIeCount() {
        $this->setPageTitle('浏览器统计');
        $id  = $this->params['id'];
        if($id) {
            $rs = DAO_Finder::findById(DB_URL_IE, $id);
            $this->set('rs', $rs);
        }

        $this->set('id', $id);
        $this->render();
    }
    
    public function actionRefererCount() {
        $this->setPageTitle('来路统计');
        $id  = $this->params['id'];
        if($id) {
            $rs = DAO_Finder::findById(DB_URL_IE, $id);
            $this->set('rs', $rs);
        }

        $this->set('id', $id);
        $this->render();
    }

    public function actionOsCount() {
        $this->setPageTitle('操作系统统计');
        $id  = $this->params['id'];
        if($id) {
            $rs = DAO_Finder::findById(DB_URL_IE, $id);
            $this->set('rs', $rs);
        }

        $this->set('id', $id);
        $this->render();
    }
    /**
     * 用户登陆
     */
    public function actionLogin() {
        if (!empty($_POST)) {
            $email = $this->postVar('email');
            $password = $this->postVar('password');
            $password = Common::getPassword($password);
            $login    = DB_USER::userLogin($email, $password);
            if($login) {
                $login  = $login->getArray();
                $_SESSION['user']   = $login;
                $_SESSION['cache']  = array();
                $ip = getIp();
                //记录登陆日志
                DB_USER_LOG::writeLog($login['id'], $ip);
                get_block('home', 'set_url_uid');
                echo 'ok';
                exit;
            }
            echo 'error';
            exit;
            //exit;
        }
        $this->view->layout = '';
        echo 'error';
    }
    
    public function actionLogout() {
        session_destroy();
        $this->redirect('/');
    }
    
    public function actionIndex() {
        $this->setPageTitle('短网址服务---缩短、分享及监测你的链接');
        $this->render();
    }



    public function actionModule() {
        $m = camelize($this->params['m']);
        $a = camelize($this->params['a']);
        $module_class = $m.'UserAdmin';
        
        $module = new $module_class;
        $params = $this->params;
        $params['module'] = $m;
        $params['action'] = $a;
        $module->setParams($params);
        $module->setView($this->view);
        $module->execute();
    }

    public function actionAjaxEdit() {
        $this->view->layout = '';

        if(!empty($_POST)) {
           $id      = $this->postVar('id');
           $rs      = DAO_Finder::findById(DB_URL, $id);
           $title   = $this->postVar('title');
           $url   = $this->postVar('url');
           if($rs){
               $rs->setTitle($title);
               $rs->setUrl($url);
               $rs->save();
               unset($_SESSION['cache']['my_url_list']);
           }
            exit;
        }

        $id = $this->params['id'];
        $rs = DAO_Finder::findById(DB_URL, $id);
        if(!$rs) {
            $rs = new DB_URL();
        }

        $this->set('rs', $rs);
        $this->render();
    }
    
    private function SetUrlUid() {
        $uhash   = Cookies::getMine('u22_url');
        if(!empty($uhash)) {
            $uid    = $_SESSION['user']['id'];
            $uhash   = "'".str_replace(',', "','", $uhash)."'";
            $sql     = "update u22_url set uid=$uid where uhash in($uhash)";
            DAO_Finder::querySQl($sql);
            Cookies::remove('u22_url');
        }
    }

    public function actionCopyUrl() {
        $this->view->layout = '';
        $id = $this->params['id'];
        $rs = DAO_Finder::findById(DB_URL, $id);
        if(!$rs) {
            $rs = new DB_URL();
        }

        $this->set('rs', $rs);
        $this->render();
    }

    public function actionAccess() {
        $this->setPageTitle('访问明细');
        $where  = array();
        $uhash  = $this->params['uhash'];
        $where['uid']  = $_SESSION['user']['id'];

        if(!empty($uhash)) {
            $where['uhash'] = $uhash;
        }
        $rs = DAO_Finder::daoAll(DB_ACCESS, $where, '*', 'id desc' ,'0,20');
        $this->set('rs', $rs);
        $this->render();
    }

    public function actionShow() {
        $this->setPageTitle('访问明细');
        $where  = array();
        $uhash  = $this->params['uhash'];
        $id     = $this->params['id'];
        $where['uid']  = $_SESSION['user']['id'];

        if(!empty($uhash)) {
            $where['uhash'] = $uhash;
        }
        $rs = DAO_Finder::daoAll(DB_ACCESS, $where, '*', 'id desc' ,'0,20');
        $this->set('rs', $rs);
        $this->set('id', $id);
        $this->render();
    }
}

